{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>
            <a href="javascript:location.reload()"> <i title="刷新" class="layui-icon layui-icon-refresh-3 child-page-refresh"></i></a>
            帖子管理
        </legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
        <a href="{:url('lists')}" class="layui-btn">显示全部</a>
<!--        <button type="button" class="layui-btn layui-btn-danger" id="delAll">批量删除</button>-->
<!--        <a href="{:url('addLists')}" class="layui-btn" style="float:right;"><i class="fa fa-plus" aria-hidden="true"></i>添加</a>-->
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
{include file="common/foot"/}
<script type="text/html" id="name">
   {{# if(d.url){ }}<img src="{{d.url}}" width="35px" onmouseover="layer.tips('<img src={{d.url}}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();">{{# } }}
</script>
<script type="text/html" id="important">
    <input type="checkbox" name="important" disabled value="{{d.important}}" lay-skin="switch" lay-text="是|否" lay-filter="important" {{ d.important == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="top">
    <input type="checkbox" name="top" disabled value="{{d.top}}" lay-skin="switch" lay-text="是|否" lay-filter="important" {{ d.top == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="putaway">
    {{# if(d.putaway == 1){ }}
    <font color="green">上架</font>
    {{# }else{ }}
    <font color="red">下架</font>
    {{# } }}
</script>
<script type="text/html" id="action">
    <a href="{:url('lookLists')}?id={{d.id}}" class="layui-btn layui-btn-xs">查看</a>
    {{# if(d.putaway == 1){ }}
    <a class="layui-btn layui-btn-xs putaway-btn" data-type="2" lay-event="putaway">下架</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger putaway-btn" data-type="1" lay-event="putaway">上架</a>
    {{# } }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
            id: 'ad',
            elem: '#list',
            url: '{:url("lists")}',
            method: 'post',
            page:true,
            cols: [[
                {field: 'id', title: 'ID', width: 60},
                {field: 'nickname', title: '发表用户', width: 100},
                {field: 'content', title: '内容', width: 300},
                {field:'category_name', title:'分类',width:100, },
                {field: 'location', title: '位置', width: 100},
                {field: 'hit', title: '浏览量', width: 100},
                {field: 'praise', title: '点赞数', width: 100},
                {field: 'comment', title: '评论数', width: 100},
                {field: 'putaway', title: '上/下架', width: 100,templet:"#putaway"},
                {field: 'create_time', title: '创建时间', width: 160},
                {width: 180,title:'操作', align: 'center', toolbar: '#action'}
            ]],
            limit:10
        });

        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            tableIn.reload({ page: {page: 1}, where: {key: key}});
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del'){
                layer.confirm('您确定要删除该条吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('delLists')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
            if (obj.event === 'putaway'){
                var putaway = obj.tr.find('.putaway-btn').data('type');
                var put = "";
                if(putaway == 1){
                    put = '上架';
                }else{
                    put = '下架';
                }
                layer.confirm('您确定要'+put+'该条数据吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('putaway')}",{id:data.id,putaway:putaway},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
            if (obj.event === 'top'){
                if(data.top === 1){
                    layer.msg('已置顶，请勿重复操作',{time:1000,icon:1});
                }else{
                    layer.confirm('您确定要置顶该条吗？', function(index){
                        var loading = layer.load(1, {shade: [0.1, '#fff']});
                        $.post("{:url('topLists')}",{id:data.id},function(res){
                            layer.close(loading);
                            if(res.code===1){
                                layer.msg(res.msg,{time:1000,icon:1});
                                tableIn.reload();
                            }else{
                                layer.msg('操作失败！',{time:1000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                }
            }
        });
        // 全部删除
        $('#delAll').click(function(){
            layer.confirm('确认要删除选中的内容吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('ad'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要删除的数据！', {time: 1000, icon: 2});
                    return false;
                }
                console.log(ids);
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delAllLists')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    })
</script>